<?php
include dirname(dirname(__FILE__)).'/admin-verify.php';
include dirname(__FILE__).'/functions.php';
?>

<!DOCTYPE html>
<html lang="zh-CN">
<head>
		<title>商品管理</title>
		<?php include dirname(dirname(dirname(__FILE__))).'/include/common-header.php' ?>
		<link rel="stylesheet" type="text/css" href="css/goods.css"/>
		<script src="../../js/jquery.ui.widget.js"></script>
		<script src="../../js/jquery.iframe-transport.js"></script>
		<script src="../../js/jquery.fileupload.js"></script>
		<script type="text/javascript">
		$(document).ready(function (){
			
			$("#image").fileupload({
			    url:"ajax/image-upload.php",//文件上传地址，当然也可以直接写在input的data-url属性内
			    formData:{},//如果需要额外添加参数可以在这里添加
			    done:function(e,result){
			        //done方法就是上传完毕的回调函数，其他回调函数可以自行查看api
			        //注意result要和jquery的ajax的data参数区分，这个对象包含了整个请求信息
			        //返回的数据在result.result中，假设我们服务器返回了一个json对象
			    	var json=JSON.parse(result.result);
			    	 if(typeof(json.error) != 'undefined'){
		                    if(json.error != ''){
		                        alert(json.error);
		                    }else{
		                    	$("#url").attr("value",json.url);
		                    	$("#store-name").attr("value",json.storeName);
			                    $("#upload-div").hide();
			                    $("#image-show").attr("src",json.url);
								$("#upload-show").show();    
		                    }
		                }
			    }
			})
			
			
			
			$("#class-name").blur(function(){

			var _className=$.trim($(this).val());
			if(_className==null||_className=='')return;
			if(_className.length<2){

				$("#class-name").parent().parent().addClass("has-error");
				$("#class-name").next().html("商品类型名称长度应大于2").fadeIn();
				$("#add-class-btn").attr("disabled","disabled");

				
				}
			else{
			$.ajax({
				dataType:"text",
				url:'ajax/is-goods-class-exists.php',
				data:{className:_className},
				success:function(data) {
					if(data=='yes') {
						$("#class-name").parent().parent().addClass("has-error");
						$("#class-name").next().html("此商品类型已存在").fadeIn();
						$("#add-class-btn").attr("disabled","disabled");
					} else {
						$("#class-name").parent().parent().removeClass("has-error")
						$("#class-name").next().hide();
						$("#add-class-btn").removeAttr("disabled");
					}
				},
			});

			}
			
			});

			
		$("input").not("#store-name").attr("maxlength","16");
		
	
		
		$(".goods-class").click(function(){
				$(".goods-class").removeClass("active");
				$(this).addClass("active");
				var _className=$.trim($(this).children().first().html());
				$("#goods-show").empty();
				$.ajax({
					dataType:"json",
					url:'ajax/show-goods.php',
					data:{className:_className},
					success:function(data) {
						$.each(data,function(name,value){
							if(value.name!=null){
							var content='<li  class="col-sm-3 goods-li" >'+
			    			'<img src="'+value.url+'" alt="..." class="img-thumbnail" >'+
			    			'<h5 class="goods-info">'+value.name+'&nbsp&nbsp每箱数量:'+value.number+'</h5>'+
			    			'<h5 class="goods-info">单价/件:'+value.piece_price+'&nbsp&nbsp单价/箱:'+value.price+'</h5>'+	
			    			'<p class="goods-info">'+value.goods_describe+'</p>'+
 						   '<p><a href="goods-edit.php?goodsName='+value.name+'" class="goods-edit btn btn-primary" role="button" >编辑</a>'+ 
 						   '<button	type="button" class="btn btn-default goods-delete" >删除</button>'+'<input style="display:none" value="'+value.name+'">'+'</p>'+
			    			'</li>'
  
							$("#goods-show").append(content);

							}

							
							});

						$(".goods-delete").on("click",function(){
						
							var _goodsName=$.trim($(this).next().val());
							var truthBeTold = window.confirm("确定要删除?");
							if (truthBeTold) {
								$.ajax({
									dataType:"text",
									url:"ajax/goods-delete.php",
									data:{goodsName:_goodsName},
									success:function(data){
										alert(data);
									},
									error:function(XMLHttpRequest, textStatus, errorThrown){
										alert("删除失败");
										
										},
									
									});
								
							} else {}

							
							
							});
					
				
						
					},
					error:function(){
						//ajax提交失败的处理函数！
						alert("获取失败");
						},
				});

				
			});

		

		$("#name").blur(function(){

			var _goodsname=$.trim($(this).val());
			if(_goodsname==null||_goodsname=='')return;
			if(_goodsname.length<2){

				$("#name").parent().parent().addClass("has-error");
				$("#name").next().html("商品名称长度应大于2").fadeIn();
				$("#add-goods-btn").attr("disabled","disabled");

				
				}
			else{
			$.ajax({
				dataType:"text",
				url:'ajax/is-goodsname-exists.php',
				data:{goodsname:_goodsname},
				success:function(data) {
					if(data=='yes') {
						$("#name").parent().parent().addClass("has-error");
						$("#name").next().html("此商品已存在").fadeIn();
						$("#add-goods-btn").attr("disabled","disabled");
					} else {
						$("#name").parent().parent().removeClass("has-error")
						$("#name").next().hide();
						$("#add-goods-btn").removeAttr("disabled");
					}
				},
			});

			}
			
			});

		$("#price").blur(function(){

			var _price = $.trim($("#price").val())

			if(isNaN(_price)){

				$("#price").parent().parent().addClass("has-error");
				$("#price").next().html("请输入数字").fadeIn();
				$("#price").val("");
				}
			else{
					if(_price<=0){
						$("#price").parent().parent().addClass("has-error");
						$("#price").next().html("数字应该大于0").fadeIn();
						$("#price").val("");
						}
					else{
						$("#price").parent().parent().removeClass("has-error");
						$("#price").next().hide();
					
						
						}
				
				}
			
	
			});



		$("#pieces-price").blur(function(){

			var _piecePrice = $.trim($("#pieces-price").val())

			if(isNaN(_piecePrice)){	

				$("#pieces-price").parent().parent().addClass("has-error");
				$("#pieces-price").next().html("请输入数字").fadeIn();
				$("#pieces-price").val("");
				}
			else{
					if(_piecePrice<=0){
						$("#pieces-price").parent().parent().addClass("has-error");
						$("#pieces-price").next().html("数字应该大于0").fadeIn();
						$("#pieces-price").val("");
						}
					else{
						$("#pieces-price").parent().parent().removeClass("has-error");
						$("#pieces-price").next().hide();
						}
				
				}
			
	
			});

		$("#number").blur(function(){

			var _number = $.trim($("#number").val())

			if(isNaN(_number)){

				$("#number").parent().parent().addClass("has-error");
				$("#number").next().html("请输入数字").fadeIn();
				$("#number").val("");
				}
			else{
					if(_number<=0){
						$("#number").parent().parent().addClass("has-error");
						$("#number").next().html("数字应该大于0").fadeIn();
						$("#number").val("");
						}
					else{
						$("#number").parent().parent().removeClass("has-error");
						$("#number").next().hide();
						}
				
				}
			
	
			});
		

		

		$("#add-goods-btn").click(function(){

			
			var _storeName=$.trim($("#store-name").val());
			var _class=$.trim($("#goods-class").val());
			if(_storeName==''||_storeName==null){
				alert("请上传图片");
				$("#name").val("");
				return;
				
				}
			if(_class==''||_class==null){
				alert("请先录入商品类型");
				$("#name").val("");
				return;
				
				}
			
			
			});

		$(".delete-class").click(function(){

			var _className=$.trim($(this).parent().parent().prev().html());
			var truthBeTold = window.confirm("确定要删除?");
			if (truthBeTold) {
			$.ajax({
				dataType:"text",
				url:"ajax/class-delete.php",
				data:{className:_className},
				success:function(data){
					alert(data);
					 location.reload();
				},
				error:function(XMLHttpRequest, textStatus, errorThrown){
					alert("删除失败")
					},
				
				});
			}
			});
		
		


		
		});
		</script>
</head>
<body>
	

	<div class="main-div">
		<ul class="nav nav-tabs" role="tablist">
		  <li class="active"><a href="#home" role="tab" data-toggle="tab">商品列表</a></li>
		  <li><a href="#add-class" role="tab" data-toggle="tab">增加类别</a></li>
		  <li><a href="#add-goods" role="tab" data-toggle="tab">增加商品</a></li>
		  <li><a href="#edit-class" role="tab" data-toggle="tab">修改类型</a></li>
		  
		</ul>
		<!-- Tab panes -->
		<div class="tab-content">
		
 <div class=" tab-pane fade in active" id="home">
		  
		  
	<div class="form-horizontal" >
				 
		<div class="form-group" >
				    
				    <div class="col-sm-2">
				    <ul class="nav nav-pills nav-stacked" role="tablist">
		 			 <?php 
		  				$items=getGoodsClass();
		  				foreach ($items as $item)
		  				{
		  					echo '<li role="presentation" class="goods-class" 	><a href="#">'.$item["name"].'</a></li>';
		  				}
		  			?>

					</ul>
				   
				    </div>
				    <div class="col-sm-10" >
				    	<ul class="goods-ul" id="goods-show">
				    		

				    	</ul>
				    	
		
				    	
				     
				</div>
		</div>			 
	</div>	  
</div>
		
		  <div class="tab-pane fade " id="add-class">
		  
		  <form class="form-horizontal" role="form" method="post" action="add-goods-class.php">
				 
			<div class="form-group" >
				    <label for="class-name" class="col-sm-2 control-label">商品类型</label>
				    <div class="col-sm-3">
				      <input required  type="text" name="class-name" class="form-control normal-input" id="class-name" placeholder="商品类别">
				       <p class="help-block" style="display:none"></p>
				    </div>
			</div>	
		
			<div class="form-group">
				    <div class="col-sm-offset-2 col-sm-10">
				      <button type="submit" class="btn btn-primary" id="add-class-btn">添加</button>
				    </div>
			</div>	
			</form>
				
				 
		</div>
		  
		  
		  
		  
		  <div class="tab-pane fade" id="add-goods">
		  
	<div class="form-horizontal" id="upload-div">
				 
		<div class="form-group" >
				    <label for="image" class="col-sm-2 control-label">图片上传</label>
				    <div class="col-sm-3">
				      <input  type="file" name="image" class="form-control normal-input" id="image">
				    </div>
		</div>	
				
				 
	</div>
	
	<div class="form-horizontal" id="upload-show" style="display:none">
				 
		<div class="form-group" >
				    <label for="image-show" class="col-sm-2 control-label">图片预览</label>
				    <div class="col-sm-3">
				    <img alt="" src="" id="image-show" class="img-thumbnail"/>
				     
				    </div>
		</div>	
		
				
				 
	</div>
	 
		  
		  
		   
			  
	<form class="form-horizontal" role="form" method="post" action="add-goods.php">
				 
		
				  <div class="form-group ">
				    <label for="name" class="col-sm-2 control-label">商品名称</label>
				    <div class="col-sm-5">
				      <input required type="text" name="name" class="form-control normal-input" id="name" placeholder="商品名称">
				      <p class="help-block" style="display:none"></p>
				    </div>
				  </div>
				  
				  <div class="form-group">
				    <label for="price" class="col-sm-2 control-label">每箱单价</label>
				    <div class="col-sm-5">
				      <input required type="text" name="price" class="form-control normal-input" id="price" placeholder="每箱单价">
						<p class="help-block" style="display:none"></p>
				      </div>
				    
				  </div>
				  
				   <div class="form-group">
				    <label for="pieces-price" class="col-sm-2 control-label">每件单价</label>
				    <div class="col-sm-5">
				      <input required type="text" name="pieces-price" class="form-control normal-input" id="pieces-price" placeholder="每件单价">
				    <p class="help-block" style="display:none"></p>
				    </div>
				  </div>
				  
				    <div class="form-group">
				    <label for="number" class="col-sm-2 control-label">每箱数量</label>
				    <div class="col-sm-5">
				      <input required type="number" name="number" class="form-control normal-input" id="number" placeholder="每箱数量">
				    </div>
				  </div>
				  
				   <div class="form-group">
				    <label for="describe" class="col-sm-2 control-label">商品描述</label>
				    <div class="col-sm-5">
				      <input required type="text" name="describe" class="form-control normal-input" id="describe" placeholder="商品描述">
				    </div>
				  </div>
				  
				  
		
				  
				   <div class="form-group">
				    <label for="goods-class" class="col-sm-2 control-label">商品种类</label>
				    <div class="col-sm-5">
				    <select class="form-control" id="goods-class" name="goods-class">
				    	 <?php 
		  					$items=getGoodsClass();
		  						foreach ($items as $item)
		  						{
		  							echo '<option value='.$item['name'].'>'.$item['name'].'</option>';
		  						}
		  				?>
					</select>
				    </div>
				  </div>
				  
				 
				 <input  type="text" name="store-name"  id="store-name" value="" style="display: none"/>
				  
				  <div class="form-group">
				    <div class="col-sm-offset-2 col-sm-10">
				      <button type="submit" class="btn btn-primary" id="add-goods-btn">添加商品</button>
				    </div>
				  </div>
				  
				</form>
		  	
		  </div>
		  
		  
		  <div class="tab-pane fade " id="edit-class">
		  
		  <div class="form-horizontal" >
				 
	
				  
			
				    
				     <?php 
		  				$items=getGoodsClass();
		  				foreach ($items as $item)
		  				{
		  					echo '<div class="form-group">'.
				  				  '<label for="describe" class="col-sm-2 control-label">'.$item["name"].'</label>'.
				    				'<div class="col-sm-5">'.
				      	'<p><a href="class-edit.php?className='.$item["name"].'" class=" btn btn-primary" role="button" >编辑</a>'.
 						'<button	type="button" class="delete-class btn btn-default " >删除</button>'.
 						'</p>'.
				    	'</div>'.
				  		'</div>';
		  				}
		  			?>
				   
				    
				  
		
				    	
				     
			
		 
	</div>	  
		 
		  
	
		  
		  </div>
		  
		  
		  
		  
		  
		</div>
	</div>
	
	
</body>
</html>